<?php
/**
 * LG
 * 1.显示静态页面
 * 2.处理登录操作
 */
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
    public function Login(){
        $this->display();
    }
    public function doLogin(){
        $User = D('User');
        if (!$User->create()){
            $this->error($User->getError());
        }else{
           $username = $_POST['username'];
           $password = $_POST['password'];
            $email ="/[a-zA-Z0-9]{1,10}@[a-zA-Z0-9]{1,5}\.[a-zA-Z0-9]{1,5}/";
            $phone = "/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/";
            $account = "/^\w{5,15}/";
            if( preg_match( $email,$username ) || preg_match( $phone,$username )|| preg_match( $account,$username )){
                $User->create();
                $rsUser = $User->getUsername();
                if($rsUser){
                    $rsPassword = $User->getPassword();
                    if($rsPassword){
                        session('id',$rsUser[0]['id'],3600);
                        cookie('username',$rsUser[0]['username'],3600);
                        $reCheck = $User->checkUser();
                        if($reCheck){
                            $this->success('欢迎回来！',U('Home/Index/Index'));//会员
                        }else{
                            $this->success('管理员，欢迎回来！',U('Admin/Admin/Admin'));//管理员跳转页面
                        }
                    }else{
                        $this->error('密码不正确！');
                    }
                }else{
                    $this->error('账号不存在！');
                }
            }else{
               $this->error('账号格式不正确！');
            }

        }
    }
    public function  LoginOut(){
        session(null); // 清空当前的session
        session('[destroy]'); // 销毁session
        cookie('username',null);//删除所有cookie
        $url = U('Home/Index/Index');
        header("location:".$url);
    }
}
?>